package com.jy.adscm.operation.newhscrb.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.newhscrb.bean.NhcMaterialDetail;
import com.jy.adscm.operation.newhscrb.bean.QueryNhcMaterialDetail;
import com.jy.adscm.operation.newhscrb.mapper.mysql.NhcMaterialDetailMapper;
import com.jy.adscm.operation.newhscrb.service.NhcMaterialDetailService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2020-06-15
 */
@Service
public class NhcMaterialDetailServiceImpl extends ServiceImpl<NhcMaterialDetailMapper, NhcMaterialDetail> implements NhcMaterialDetailService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public NhcMaterialDetail insertnhcMaterialDetail(NhcMaterialDetail nhcMaterialDetail) {
        synchronized (this){
            String date =new SimpleDateFormat("yyyyMMdd").format(new Date());
            Wrapper<NhcMaterialDetail> wrapper=new EntityWrapper<>();
            wrapper.like("code",date, SqlLike.RIGHT);
            wrapper.orderBy("code",false);
            wrapper.last(" limit 1");
            List<NhcMaterialDetail> nhcMaterialDetails = super.selectList(wrapper);
            String  code ="";
            if(nhcMaterialDetails!=null&& nhcMaterialDetails.size()>0){
                NhcMaterialDetail old = nhcMaterialDetails.get(0);
                code = String.valueOf(Long.valueOf(old.getCode())+1) ;
            }else {
                code=date+"0001";
            }
            nhcMaterialDetail.setCode(code);
            boolean insert = super.insert(nhcMaterialDetail);
            return nhcMaterialDetail;
        }
    }

    @Override
    public void printCode(String code) {

    }

    @Override
    public boolean updateNhcMaterialDetail(Token token, NhcMaterialDetail nhcMaterialDetail) {
        NhcMaterialDetail oldNhcMaterialDetail = super.selectById(nhcMaterialDetail.getId());
        if (AdscmBaseUtils.getInstance().isNotEmpty(nhcMaterialDetail.getWeight(),null)){
            if (!AdscmBaseUtils.getInstance().isNotEmpty(oldNhcMaterialDetail.getWeight(),null)){
                nhcMaterialDetail.setCreator(token.getUser().getId().toString());
            }
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(nhcMaterialDetail.getOutStockDate(),null)){
            if (!AdscmBaseUtils.getInstance().isNotEmpty(oldNhcMaterialDetail.getOutStockDate(),null)){
                nhcMaterialDetail.setOutStocker(token.getUser().getId().toString());
            }
        }
        boolean b = super.updateById(nhcMaterialDetail);
        return b;
    }

    @Override
    public Page<NhcMaterialDetail> selectByPid(QueryNhcMaterialDetail queryNhcMaterialDetail) {
        Page<NhcMaterialDetail> page=new Page<>(queryNhcMaterialDetail.getCurrentPage(),queryNhcMaterialDetail.getPageSize());
        Wrapper<NhcMaterialDetail> wrapper=new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryNhcMaterialDetail.getCode(),null)){
            wrapper.eq("code",queryNhcMaterialDetail.getCode());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryNhcMaterialDetail.getResult(),null)){
            wrapper.isNotNull("result");
        }else {
            wrapper.isNull("result");
        }
        wrapper.eq("pid",queryNhcMaterialDetail.getPid());
        wrapper.orderBy("create_time",false);
        page= super.selectPage(page,wrapper);
        List<NhcMaterialDetail> nhcMaterialDetails = page.getRecords();
        for (NhcMaterialDetail nhcMaterialDetail : nhcMaterialDetails) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(nhcMaterialDetail.getOutStocker(),null)){
                JyUser jyUser = jyUserMapper.selectById(nhcMaterialDetail.getOutStocker());
                nhcMaterialDetail.setOutStockerName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(nhcMaterialDetail.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(nhcMaterialDetail.getCreator());
                nhcMaterialDetail.setCreatorName(jyUser.getName());
            }
        }
        page.setTotal(selectCount(wrapper));
        return page;
    }

    @Override
    public String getMaxCode() {
        String date =new SimpleDateFormat("yyyyMMdd").format(new Date());
        Wrapper<NhcMaterialDetail> wrapper=new EntityWrapper<>();
        wrapper.like("code",date, SqlLike.RIGHT);
        wrapper.orderBy("code",false);
        wrapper.last(" limit 1");
        List<NhcMaterialDetail> nhcMaterialDetails = super.selectList(wrapper);
        String  code ="";
        if(nhcMaterialDetails!=null&& nhcMaterialDetails.size()>0){
            NhcMaterialDetail old = nhcMaterialDetails.get(0);
            code = String.valueOf(Long.valueOf(old.getCode())+1) ;
        }else {
            code=date+"0001";
        }
        return code;
    }

    @Override
    public Boolean outPutMaterial(String code, Integer result, Token token) {
        Wrapper<NhcMaterialDetail> nhcMaterialDetailWrapper=new EntityWrapper<>();
        nhcMaterialDetailWrapper.eq("code",code);
        List<NhcMaterialDetail> nhcMaterialDetails = super.selectList(nhcMaterialDetailWrapper);
        if (nhcMaterialDetails!=null&&nhcMaterialDetails.size()>0){
            NhcMaterialDetail nhcMaterialDetail = nhcMaterialDetails.get(0);
            nhcMaterialDetail.setOutStocker(token.getUser().getId().toString());
            nhcMaterialDetail.setOutStockDate(new Date());
            nhcMaterialDetail.setResult(result);
            super.updateById(nhcMaterialDetail);
        }else {
            return false;
        }
        return true;
    }
}
